// 存储适配器，根据平台选择不同的存储方式
class StorageAdapter {
	constructor() {
		this.storage = this.getStorage();
	}

	// 根据平台获取存储对象
	getStorage() {
		// #ifdef H5
		return localStorage;
		// #endif

		// #ifdef MP-WEIXIN || MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || MP-QQ
		return {
			getItem: (key) => {
				try {
					return uni.getStorageSync(key);
				} catch (e) {
					console.error('Storage getItem error:', e);
					return null;
				}
			},
			setItem: (key, value) => {
				try {
					uni.setStorageSync(key, value);
				} catch (e) {
					console.error('Storage setItem error:', e);
				}
			},
			removeItem: (key) => {
				try {
					uni.removeStorageSync(key);
				} catch (e) {
					console.error('Storage removeItem error:', e);
				}
			},
			clear: () => {
				try {
					uni.clearStorageSync();
				} catch (e) {
					console.error('Storage clear error:', e);
				}
			}
		};
		// #endif
	}

	// 获取存储值
	getItem(key) {
		return this.storage.getItem(key);
	}

	// 设置存储值
	setItem(key, value) {
		this.storage.setItem(key, value);
	}

	// 移除存储值
	removeItem(key) {
		this.storage.removeItem(key);
	}

	// 清空存储
	clear() {
		this.storage.clear();
	}
}

export default new StorageAdapter();